package onlinebanking.client.gui;

import java.awt.Color;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import onlinebanking.client.ServerListener;
import onlinebanking.client.SimpleClient;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ClientPanel extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BottomPanel BottomTabs;

	private Sidebar sidebar;
	
	private JLabel headerLabel;
	private JPanel headerPanel;
	private JLabel copyright;
	private HyperlinkJLabel termsOfUse;
	private JLabel UsernameLabel;
	private JLabel StatementOfAccountLabel;
	private JPanel StatementOfAccountPanel;

	private HyperlinkJLabel surfGuides;
	private HyperlinkJLabel aboutUs;
	private HyperlinkJLabel services;

	private JPanel modulePanel;

	public AnnouncementsPanel announcementsPanel;
	public AccountBalancePanel accountBalancePanel;
	public InitiateTransactionPanel initiateTransactionPanel;
	public ConfirmTransactionPanel confirmTransactionPanel;
	public ConfirmTransactionDetailsPanel ConfirmTransactionDetailsPanel;
	public AccountStatementPanel accountStatementPanel;
	public AccountSettingsPanel accountSettingsPanel;
	public LoginPanel loginPanel;
	public LogoutPanel logoutPanel;
	//private HashMap<Integer, JPanel> moduleMap;
	
	private SimpleClient simpleclient;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		
		

		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ClientPanel inst = new ClientPanel();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public ClientPanel() {
		super();
		startSimpleClient();
		initGUI();
		// initialize default UI settings
		this.disableButtons();
		this.defaultModule();
		
	}

	public void processWindowEvent(WindowEvent w) {
		if (w.getID() == WindowEvent.WINDOW_CLOSING) {
			int decision = JOptionPane.showConfirmDialog(this,
					"Are you sure you want to close this window?",
					"Confirm Close", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if (decision == JOptionPane.YES_OPTION) {
				dispose();
				simpleclient.disconnect();
				System.exit(0);
			}
		} else {
			super.processWindowEvent(w);
		}
		
		
	}

	private void initGUI() {
		try {
			// initialize the modules //TODO:
			//initializeModules();

			getContentPane().setLayout(null);
			getContentPane().setBackground(new java.awt.Color(190, 255, 174));
			this.setTitle("PDF Bank");
			this.setResizable(false);
			{
				headerPanel = new JPanel();
				getContentPane().add(headerPanel);
				headerPanel.setBounds(6, 11, 772, 55);
				headerPanel.setLayout(null);
				headerPanel.setBackground(new java.awt.Color(190, 255, 174));
				{
					logoutPanel = new LogoutPanel(this);
					headerPanel.add(logoutPanel);
					logoutPanel.setBounds(275, 19, 497, 49);
					logoutPanel.setLayout(null);
					logoutPanel.setVisible(false);
					logoutPanel.setBackground(new java.awt.Color(190,255,174));
				}
				{
					headerLabel = new JLabel();
					headerPanel.add(headerLabel);
					headerLabel.setText("PDF BANK");
					headerLabel.setBounds(0, 0, 275, 50);
					headerLabel.setEnabled(false);
					headerLabel
							.setFont(new java.awt.Font("Arial Black", 0, 48));
					headerLabel
							.setForeground(new java.awt.Color(230, 251, 238));
				}
				{
					loginPanel = new LoginPanel(this);
					headerPanel.add(loginPanel);
					loginPanel.setBounds(287, 16, 485, 40);
					loginPanel.setLayout(null);
					loginPanel.setBackground(new java.awt.Color(190, 255, 174));
					{
						UsernameLabel = new JLabel();
						headerPanel.add(UsernameLabel);
						UsernameLabel.setText("");
						UsernameLabel.setBounds(281, -10, 491, 34);
						UsernameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
					}
					
				}
			}
			{
				sidebar = new Sidebar(this);
				getContentPane().add(sidebar);
				sidebar.setBounds(6, 72, 192, 450);
				sidebar.setLayout(null);
				sidebar.setBackground(new java.awt.Color(190, 255, 174));
			}
			{
				BottomTabs = new BottomPanel();
				getContentPane().add(BottomTabs);
				BottomTabs.setBounds(8, 528, 774, 31);
				BottomTabs.setBackground(new java.awt.Color(192, 192, 192));
				BottomTabs.setLayout(null);
				BottomTabs.setPreferredSize(new java.awt.Dimension(775, 30));
				{
					termsOfUse = new HyperlinkJLabel("Terms Of Use");
					BottomTabs.add(termsOfUse);
					termsOfUse.setBounds(6, 5, 100, 15);
				}
				{
					surfGuides = new HyperlinkJLabel("Surfing Guidelines");
					BottomTabs.add(surfGuides);
					surfGuides.setBounds(112, 5, 105, 15);
				}
				{
					aboutUs = new HyperlinkJLabel("About Us");
					BottomTabs.add(aboutUs);
					aboutUs.setBounds(218, 5, 100, 15);
				}
				{
					services = new HyperlinkJLabel("Services");
					BottomTabs.add(services);
					services.setBounds(346, 5, 100, 15);
				}
				{
					copyright = new JLabel(
							"Copyright 2011. All rights reserved.");
					copyright.setForeground(Color.BLUE);
					BottomTabs.add(copyright);
					copyright.setBounds(568, 5, 200, 15);
				}
			}

			pack();
			this.setSize(798, 597);

			
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	private void initializeModules() {
		this.announcementsPanel = new AnnouncementsPanel(this);
		this.accountBalancePanel = new AccountBalancePanel(this);
		this.initiateTransactionPanel = new InitiateTransactionPanel(this);
		this.confirmTransactionPanel = new ConfirmTransactionPanel(this);
		this.accountStatementPanel = new AccountStatementPanel(this);
		this.accountSettingsPanel = new AccountSettingsPanel(this);
		
	}

	/**
	 * Shows the default module of the application.
	 */
	public void defaultModule() {
		this.switchToAnnoucements();
	}

	/**
	 * Initialises an application module.
	 * 
	 * @param module
	 *            the module to be initialised.
	 */
	private void initializeModule(JPanel module) {
		if(this.modulePanel != null){
			getContentPane().remove(this.modulePanel);
		}			
		getContentPane().add(module);
		module.setBackground(new java.awt.Color(190, 255, 174));
		module.setBounds(200, 72, 590, 450);
		module.setLayout(null);
		module.setVisible(true); // it should be hidden at first
	}
	
	

	
	
	void switchToAnnoucements(){
		this.announcementsPanel = new AnnouncementsPanel(this);			
		this.initializeModule(this.announcementsPanel);
		this.modulePanel = this.announcementsPanel;
	}
	
	void switchToAccountBalance(){
		this.accountBalancePanel = new AccountBalancePanel(this);			
		this.initializeModule(this.accountBalancePanel);
		this.modulePanel = this.accountBalancePanel;
	}
	
	void switchToInitiateTransaction(){
		this.initiateTransactionPanel = new InitiateTransactionPanel(this);			
		this.initializeModule(this.initiateTransactionPanel);
		this.modulePanel = this.initiateTransactionPanel;
	}
	
	void switchToConfirmTransaction(){
		this.confirmTransactionPanel = new ConfirmTransactionPanel(this);			
		this.initializeModule(this.confirmTransactionPanel);
		this.modulePanel = this.confirmTransactionPanel;
	}
	
	void switchToConfirmTransactionDetail(int transactionid){
		this.ConfirmTransactionDetailsPanel = new ConfirmTransactionDetailsPanel(this,transactionid);
		this.initializeModule(this.ConfirmTransactionDetailsPanel);
		this.modulePanel = this.ConfirmTransactionDetailsPanel;
	}
	
	void switchToStatementOfAccount(){
		JOptionPane.showMessageDialog(null, "implement on Clientpanel");
	}
	

	public void disableButtons() {
		this.sidebar.getAnnouncementsButton().setEnabled(false);
		this.sidebar.getAccountBalanceButton().setEnabled(false);
		this.sidebar.getInitiateTransactionButton().setEnabled(false);
		this.sidebar.getConfirmTransactionButton().setEnabled(false);
		this.sidebar.getAccountStatementButton().setEnabled(false);
		this.sidebar.getAccountSettingsButton().setEnabled(false);
	}

	public void enableButtons() {
		this.sidebar.getAnnouncementsButton().setEnabled(true);
		this.sidebar.getAccountBalanceButton().setEnabled(true);
		this.sidebar.getInitiateTransactionButton().setEnabled(true);
		this.sidebar.getConfirmTransactionButton().setEnabled(true);
		this.sidebar.getAccountStatementButton().setEnabled(true);
		this.sidebar.getAccountSettingsButton().setEnabled(true);
	}
	
	public void showLoginPanel(boolean showlogin){
		this.loginPanel.setVisible(showlogin);
		this.logoutPanel.setVisible(!showlogin);
		//if logged in, 
		if(!showlogin){
			//request list of bank accounts
			this.simpleclient.getListOfMyBankAccounts();
		}
	}
	
	public void setUsername(String username){
		this.UsernameLabel.setText(username);
	}

	public SimpleClient getSimpleClient() {
		return simpleclient;
	}
	
	void startSimpleClient(){
		try {
			System.out.println("Starting Simple Client");
			this.simpleclient = new SimpleClient();
			ServerListener serverlistener = this.simpleclient.getServerListener();
			serverlistener.setClientPanel(this);
			return;
	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Error in starting SimpleClient " + e);
			e.printStackTrace();
			javax.swing.JOptionPane.showMessageDialog(null,
					"Error in connecting to server.");
	
			// quit GUI
			System.exit(1);
		}
	}

}
